﻿using Microsoft.Win32;
using NEM.Networking_Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace  NEM
{
    [Serializable]
    class NetworkProfileClass
    {
        #region Windows API Functions
        //Use Windows API Function to Refresh Proxy settings

        [DllImport("wininet.dll")]
        public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
        public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
        public const int INTERNET_OPTION_REFRESH = 37;
        bool settingsReturn, refreshReturn;

        #endregion End Windows API Functions

        #region Private Variables / Constants

        #endregion

        #region Constructors
        public NetworkProfileClass()
        {

        }
        #endregion

        #region Properties

        #region Network Profile Information

        private String _ProfileName = "New Profile";
        [Category("0 - Network Profile Information")]
        [DisplayName("1 - Name")]
        [Description("Name of the Network Profile.")]
        public String ProfileName
        {
            get { return _ProfileName; }
            set { _ProfileName = value; }
        }

        private String _PofileDescription = string.Empty;
        [Category("0 - Network Profile Information")]
        [DisplayName("2 - Description")]
        [Description("Information about this Profile.")]
        public String ProfileDescription
        {
            get { return _PofileDescription; }
            set { _PofileDescription = value; }
        }

        private String _Notes = string.Empty;
        [Category("0 - Network Profile Information")]
        [DisplayName("3 - Profile Notes")]
        [Description("Extra Notes for this Profile.")]
        [Editor(typeof(FancyStringEditor), typeof(UITypeEditor))]
        public String Variable
        {
            get { return _Notes; }
            set { _Notes = value; }
        }
        #endregion End of Network Profile Information.

        #region Network Card Info

        private NetworkConnection _NetworkConnection = new NetworkConnection();
        [Category("1 - Network Connection Info")]
        [DisplayName("Network Connection")]
        [Description("Network Connection")]
        [Browsable(false)]
        public NetworkConnection NetwrokConnection
        {
            get { return _NetworkConnection; }
            set { _NetworkConnection = value; }
        }

        private IPAddressInfo _IPAddressInfo = new IPAddressInfo();
        [TypeConverter(typeof(ExpandableObjectConverter))]
        [Category("1 - Network Connection Info")]
        [DisplayName("IP Address Information")]
        [Description("Settings for the IP Address")]
        public IPAddressInfo IPAddressInfo
        {
            get { return _IPAddressInfo; }
            set { _IPAddressInfo = value; }
        }

        #endregion end of Network card Info

        #region Proxy Information
        private ProxyInfo _NetworkProfileSettings = new ProxyInfo();
        [Category("2 - Proxy Information")]
        [DisplayName("Proxy Information")]
        [Description("Information that will be used for Proxy.")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public ProxyInfo ProxyInformation
        {
            get { return _NetworkProfileSettings; }
            set { _NetworkProfileSettings = value; }
        }

        #endregion End Proxy Information

        #region Notification Settings

        private bool _AllowNotifications = Globals.ApplicationSettings.NotificationSettings.Enable;
        [Category("3 - Notification")]
        [DisplayName("1 - Enable Notifications")]
        [Description("Enable/Disable Notifications pop-ups for this Profile")]
        public bool AllowNotification
        {
            get { return _AllowNotifications; }
            set { _AllowNotifications = value; }
        }

        private int _NotificationShowTime = Globals.ApplicationSettings.NotificationSettings.ShowTIme;
        [Category("3 - Notification")]
        [DisplayName("2 - Show Time")]
        [Description("Time in Milliseconds for the pop-up to show.")]
        public int NotificationShowTime
        {
            get { return _NotificationShowTime; }
            set { _NotificationShowTime = value; }
        }

        private String _DisplayMessage = string.Empty;
        [Category("3 - Notification")]
        [DisplayName("3 - Display Message")]
        [Description("Extra information to be displayed in the pop-up.")]
        public String DisplayMessage
        {
            get { return _DisplayMessage; }
            set { _DisplayMessage = value; }
        }

        #endregion End of Notification Settings.


        #region Auto Activation Settings
        private AutoActivate _AutoActivateSettings = new AutoActivate();
        [TypeConverter(typeof(ExpandableObjectConverter))]
        [Category("4 - Auto Activate Profile")]
        [DisplayName("AutoActivate Settings")]
        [Description("Settings to allow this profile to AutoActivate.")]
        public AutoActivate AutoActivateSettings
        {
            get { return _AutoActivateSettings; }
            set { _AutoActivateSettings = value; }
        }

        #endregion End of Auto Activation Settings

        #endregion End of Properties

        #region Methods
        /// <summary>
        /// Activate Network Profile
        /// </summary>
        public void AcitvateProfile()
        {

            //SetIP Address
            
            NetwrokConnection.SetIPSettings(IPAddressInfo);

            try
            {
                RegistryKey lvRegKey = Registry.CurrentUser.OpenSubKey(@"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
                lvRegKey.SetValue("ProxyServer", ProxyInformation.IPAddress + ":" + ProxyInformation.Port);
                lvRegKey.SetValue("ProxyEnable", ProxyInformation.EnableProxy ? 1 : 0);
                settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
                refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Validates the needed properties to make this Network profile usable.
        /// </summary>
        /// <param name="pReturnMessage">Returns any error message for use to see.</param>
        /// <returns>bool if Network profile is valid.</returns>
        public bool Validate(out string pReturnMessage)
        {
            bool lvResult = true;
            StringBuilder lvStringBuilder = new StringBuilder();
            string lvTempValidateMessage = string.Empty;

            //Validate Proxy Information.
            if (!ProxyInformation.Validate(out lvTempValidateMessage))
            {
                lvResult = false;
                lvStringBuilder.Append(lvTempValidateMessage + Environment.NewLine);
            }

            //Validate IP Information
            if (!IPAddressInfo.Validate(out lvTempValidateMessage))
            {
                lvResult = false;
                lvStringBuilder.Append(lvTempValidateMessage + Environment.NewLine);
            }

            
            #region Validate Local Properties
            //Validate local Properties
            #endregion

            pReturnMessage = lvStringBuilder.ToString();
            return lvResult;
        }

        #endregion

        #region Overrides
        public override string ToString()
        {
            return ProfileName != string.Empty ? ProfileName : "Setup Network Profile";
        }
        #endregion

    }



    public class AutoActivate
    {
        #region Private Variables / Constants

        #endregion

        #region Constructors
        public AutoActivate()
        {

        }
        #endregion

        #region Properties

        private bool _Enable = false;
        [Category("Auto Activate Settings")]
        [DisplayName("1 - Enable")]
        [Description("Enable Auto Activate for this Profile.")]
        public bool Enable
        {
            get { return _Enable; }
            set { _Enable = value; }
        }

        private NetworkIdentity _NetworkIdentity= new NetworkIdentity();
        [Category("Network Identity")]
        [DisplayName("Network Identity")]
        [Description("Network Identity will be used to Auto Activate a Profile.")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public NetworkIdentity NetworkIdentity
        {
            get { return _NetworkIdentity; }
            set { _NetworkIdentity = value; }
        }

        #endregion

        #region Methods
        
        #endregion

        #region Overrides
        public override string ToString()
        {
            return "Setup Auto Activation";
        }
        #endregion
    }


    public class NetworkIdentity
    {
        #region Private Variables / Constants

        #endregion

        #region Constructors
        public NetworkIdentity()
        {

        }
        #endregion

        #region Properties
        private String _dnsSuffix = string.Empty;
        [Category("DNS Information")]
        [DisplayName("DNS Suffix Name")]
        [Description("The name of the DNS Suffix.")]
        public String DnsSuffixName
        {
            get { return _dnsSuffix; }
            set { _dnsSuffix = value; }
        }

        private String _DhcpAddress = string.Empty;
        [Category("DHCP Information")]
        [DisplayName("DHCP Address")]
        [Description("The Address of the DHCP Server.")]
        public String DhcpAddress
        {
            get { return _DhcpAddress; }
            set { _DhcpAddress = value; }
        }

        private String _DhcpMacAddress = string.Empty;
        [Category("DHCP Information")]
        [DisplayName("DHCP MacAddress")]
        [Description("The Mac Address of the DHCP Server.")]
        public String DhcpMacAddress
        {
            get { return _DhcpMacAddress; }
            set { _DhcpMacAddress = value; }
        }

        #endregion

        #region Methods

        #endregion

        #region Overrides
        public override string ToString()
        {
            return "Setup Network Identity";
        }
        #endregion
    }

}